window.onload = function () {
    console.log('af');
    var ul = this.document.querySelector('#demo');
    var bou2 = this.document.querySelector('#secondbou');
    var bou1 = this.document.querySelector('#firstbou');
    var text = this.document.querySelector('textarea');
    var fip = this.document.querySelector('#firstIn');
    var li = ul.querySelectorAll('ul li');
    bou2.onclick = function () {
        if (text.value.length == 0) {
            alert('请先输入你的评论!!!');
        } else {
            var z = confirm('确定要发布该条评论吗？');
            if (z == true) {
                var li = document.createElement('li');
                var div = document.createElement('div');
                div.innerHTML = text.value;
                li.appendChild(div);
                var a1 = document.createElement('a');
                a1.innerHTML = '删除';
                a1.setAttribute('class', 'a1');
                li.appendChild(a1);
                var a2 = document.createElement('a');
                a2.innerHTML = '修改';
                a2.setAttribute('class', 'a2');
                li.appendChild(a2);
                // li.innerHTML = text.innerHTML+"<a href='javascript:;' class='a1'>删除</a>"+"<a href='javascript:;' class='a2'>修改</a>";
                text.value = '';
                ul.insertBefore(li, ul.children[0]);
                var trans = document.querySelectorAll('.a2');
                for (var i = 0; i < trans.length; i++) {
                    trans[i].addEventListener('click', function () {
                        var x = confirm('确定要进行修改吗？');
                        if (x == true) {
                            var word = prompt('请再次重新输入你的评论！'); 
                            if (word.length == 0) {
                                alert('未进行修改');
                            } else {
                                this.parentNode.children[0].innerHTML = word;
                            }
                        }
                    })
                }
                var window = document.querySelector('#window');
                var s = document.querySelector('#sure');
                var c = window.querySelector('#cancel');
                var dels = document.querySelectorAll('.a1');
                var flag = 0;
                var body = document.querySelector('body');
                 function remov(i){
                       ul.removeChild(ul.childNodes[i]);
                    }
                for (var i = 0; i < dels.length; i++){
                    var num = i;
                    dels[num].onclick = function (){
                        body.style.overflow = 'hidden';
                        window.style.display = 'block';
                        s.addEventListener('click',function(){
                            window.style.display = 'none';
                          flag = 1;    
                          body.style.overflow = '';
                          ul.removeChild(ul.childNodes[num]);
                              //  remov(i);
                        })  
                        
                  
                        c.addEventListener('click', function () {
                            flag = 0;
                            window.style.display = 'none';
                            body.style.overflow = '';
                        })
                    }
                }
            }
        }
    }
//    var li = ul.querySelectorAll('ul li');
    bou1.addEventListener('click', function () {
        for (var i = 0; i < ul.children.length; i++) {
            ul.children[i].style.backgroundColor = 'white';
        }
        var temp = 0;
        for (var i = 0; i < ul.children.length; i++) {
            if (fip.value == ul.children[i].children[0].innerHTML) {
                ul.children[i].style.backgroundColor = 'skyblue';
                temp = 1;
            }
        }
        if (temp == 0) {
            alert('未找到想要评论！');
        }
    })
}